package com.suedawn.bookmanagesystem.controller;

import com.suedawn.bookmanagesystem.entity.Category;
import com.suedawn.bookmanagesystem.service.CategoryService;
import com.suedawn.bookmanagesystem.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @GetMapping("/list")
    public R list(){
        List<Category> list = categoryService.list();
        return R.ok(list);
    }

    @DeleteMapping("/del")
    public R del(int categoryId){
        boolean b = categoryService.removeById(categoryId);
        return b?R.ok():R.error();
    }

    @PostMapping("/save")
    public R save(@RequestBody Category category){
        boolean b = categoryService.save(category);
        return b?R.ok():R.error();
    }

    @PutMapping("/update")
    public R update(@RequestBody Category category){
        boolean b = categoryService.updateById(category);
        return b?R.ok():R.error();
    }
}
